package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.mc.developmentkit.json.Json;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AboutUs;
import com.wude.bean.ShareInfo;
import com.wude.guild.R;
import com.wude.guild.dialog.SharedDialog;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 问题与建议
 */

public class AdviseFragment extends TitleBaseFragment implements View.OnClickListener {
    private Unbinder unbinder;
    @BindView(R.id.et_content)
    EditText et_content;//输入的问题描述
    @BindView(R.id.et_contactWay)
    EditText et_contactWay;
    @BindView(R.id.tv_commit)
    TextView tv_commit;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.advise_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("AdviseFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("AdviseFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        setHeaderTitle("问题与建议");

        tv_commit.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View view) {
        String content = et_content.getText().toString();
        if (!TextUtils.isEmpty(content)) {
            String s = content.replaceAll("\\s*", "");
            if (TextUtils.isEmpty(s)) {
                ToastUtil.showToast("请输入您遇到的问题或建议");
                return;
            }
        } else {
            ToastUtil.showToast("请输入您遇到的问题或建议");
            return;
        }
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("token", Utils.getLoginUser().token);
        dataMap.put("content", et_content.getText().toString());
        dataMap.put("contact", et_contactWay.getText().toString());
        HttpCom.POST(bhandler, HttpCom.getQuestionSuggest(), dataMap, false);
    }

    /**
     * 获取分享内容
     */
    @SuppressLint("HandlerLeak")
    Handler bhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String return_code = jsonObject.getString("return_code");
                        ToastUtil.showToast(return_code);
                        if (status == 1) {//提交成功
                            getActivity().finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络连接失败");
                    break;
            }
        }
    };
}
